package com.lottery.game.core.modules.service.redis.impl;

import com.lottery.game.core.converter.ObjectTranscoder;
import com.lottery.game.core.modules.service.redis.RedisService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import lombok.extern.slf4j.Slf4j;

/**
 * @author : yinlin
 * @version : 1.0
 * @date : 2019-03-28 10:16
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : EasySite Technology Chengdu Co. Ltd.
 * @link : com.lottery.game.core.modules.service.redis.impl.RedisServiceImpl
 */
@Service
@Slf4j
public class RedisServiceImpl implements RedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public void set(String key, String value) {
        redisTemplate.opsForValue().set(key, value);
    }

    @Override
    public void set(String key, String value, int time) {
        redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
    }

    @Override
    public void delete(String key) {
        redisTemplate.delete(key);
    }

    @Override
    public String get(String key) {
        return redisTemplate.opsForValue().get(key);
    }

    @Override
    public Set<String> keys(String keys) {
        return redisTemplate.keys(keys);
    }

    @Override
    public <T> void setList(String key, List<T> list) {
        Object[] t = list.toArray();
        redisTemplate.opsForValue().set(key, new String(ObjectTranscoder.serialize(t)));
    }

    @Override
    public <T> List<T> getList(String key) {
        String r = redisTemplate.opsForValue().get(key);
        return (List<T>) ObjectTranscoder.deserialize(r.getBytes());
    }
}
